<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(APPPATH . 'models/model_grup.php');
require_once(APPPATH . 'models/model_pesan.php');
require_once(APPPATH . 'models/kotak/kotak_masuk.php');
require_once(APPPATH . 'models/kotak/kotak_keluar.php');
require_once(APPPATH . 'models/kotak/kotak_terkirim.php');

class Sms extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$this->load->view('theme/theme_kepala');
		$this->load->view('utama');
		$this->load->view('theme/theme_kaki');
	}

	# ================== KIRIM SMS ======================

	public function kirim_manual($nomor_tujuan = "")
	{
		$data['nomor_tujuan'] = $nomor_tujuan;

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_kirim_manual', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function kirim_manual_proses()
	{
		$data['kirim_mode'] = 'manual_proses';

		$pesan = new model_pesan($this->input->post());
		$pesan->kirim();
		
		$data['sms_jumlah'] = count($pesan->get_penerima())*count($pesan->get_pesan_isi());

		$this->load->view('theme/theme_kepala');
		$this->load->view('utama', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function kirim_grup()
	{
		$data['opsi_pengiriman_array'] = model_grup::read();
		$data['url'] = site_url('sms/kirim_grup_proses');
		$data['label'] = 'Grup';

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_kirim_multi', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function kirim_grup_proses()
	{
		$data['TextDecoded'] = $this->input->post('TextDecoded');

		$kontak_array = model_grup::read_anggota($this->input->post('id'));

		foreach ($kontak_array as $kontak)
			if ($kontak->get_nomor()!=="")
				$nomor[] = $kontak->get_nomor();

		// menghindari duplikasi nomor
		$nomor = array_unique($nomor);

		$data['DestinationNumber'] = implode(',', $nomor);

		$pesan = new model_pesan($data);
		$pesan->kirim();

		$data['sms_jumlah'] = count($pesan->get_penerima())*count($pesan->get_pesan_isi());

		$this->load->view('theme/theme_kepala');
		$this->load->view('utama', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function kirim_kontak()
	{
		$data['opsi_pengiriman_array'] = model_kontak::read();
		$data['url'] = site_url('sms/kirim_kontak_proses');
		$data['label'] = 'Nama';

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_kirim_multi', $data);
		$this->load->view('theme/theme_kaki');
	}

	public function kirim_kontak_proses()
	{
		$data['TextDecoded'] = $this->input->post('TextDecoded');

		$kontak_array = model_kontak::read(array('pbk_id'=>$this->input->post('id')));

		foreach ($kontak_array as $kontak)
			if ($kontak->get_nomor()!=="")
				$nomor[] = $kontak->get_nomor();

		// menghindari duplikasi nomor
		$nomor = array_unique($nomor);

		$data['DestinationNumber'] = implode(',', $nomor);

		$pesan = new model_pesan($data);
		$pesan->kirim();

		$data['sms_jumlah'] = count($pesan->get_penerima())*count($pesan->get_pesan_isi());

		$this->load->view('theme/theme_kepala');
		$this->load->view('utama', $data);
		$this->load->view('theme/theme_kaki');
	}

	# ================== INBOX ======================

	public function inbox($mulai = 0)
	{
		$kotak_masuk = new kotak_masuk();
		$config = $this->config->item('pagination');
		//$data['kotak'] = $kotak_masuk->ambil();	

		$config['base_url']=  site_url('/sms/inbox/');
		$config['total_rows']=  kotak_masuk::count();
		$config['per_page']=  50;

		$this->pagination->initialize($config);

		$data['halaman'] = $this->pagination->create_links();
		$data['mulai'] = $mulai;
		$data['kotak'] = $kotak_masuk->ambil(array(), array($config['per_page'], $mulai), '*', false, "", "ReceivingDateTime DESC");

		$this->load->library('table');

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_inbox', $data);
		$this->load->view('theme/theme_kaki');
	}

	# ================== OUTBOX ======================

	public function outbox($mulai = 0)
	{
		$kotak_keluar = new kotak_keluar();
		$config = $this->config->item('pagination');
		//$data['kotak'] = $kotak_keluar->ambil();	

		$config['base_url']=  site_url('/sms/outbox/');
		$config['total_rows']=  kotak_keluar::count();
		$config['per_page']=  50;

		$this->pagination->initialize($config);

		$data['halaman'] = $this->pagination->create_links();
		$data['mulai'] = $mulai;
		$data['kotak'] = $kotak_keluar->ambil(array(), array($config['per_page'], $mulai), '*', false, "", "SendingDateTime DESC");


		$this->load->library('table');

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_outbox', $data);
		$this->load->view('theme/theme_kaki');
	}

	# ================== SENT ITEMS ======================

	public function sentbox($mulai = 0)
	{
		$kotak_terkirim = new kotak_terkirim();
		$config = $this->config->item('pagination');
		//$data['kotak'] = $kotak_terkirim->ambil();	

		$config['base_url']=  site_url('/sms/sentbox/');
		$config['total_rows']=  kotak_terkirim::count();
		$config['per_page']=  50;

		$this->pagination->initialize($config);

		$data['halaman'] = $this->pagination->create_links();
		$data['mulai'] = $mulai;
		$data['kotak'] = $kotak_terkirim->ambil(array(), array($config['per_page'], $mulai), '*', false, "", "SendingDateTime DESC");

		// Membuat array status dalam bahasa Indonesia
		$data['status'] = array(
			'SendingOK'=>"<span class='label label-info'>Terkirim</span>",
			'SendingOKNoReport'=>"<span class='label label-info'>Tanpa Laporan</span>",
			'SendingError'=>"<span class='label label-danger'>Pengiriman Gagal</span>",
			'DeliveryOK'=>"<span class='label label-success'>Diterima</span>",
			'DeliveryFailed'=>"<span class='label label-danger'>Gagal Diterima</span>",
			'DeliveryPending'=>"<span class='label label-info'>Menunggu</span>",
			'DeliveryUnknown'=>"<span class='label label-warning'>Nggak Tau</span>",
			'Error'=>"<span class='label label-danger'>Kegagalan Sistem</span>",
		);

		$this->load->library('table');

		$this->load->view('theme/theme_kepala');
		$this->load->view('sms/sms_sentbox', $data);
		$this->load->view('theme/theme_kaki');
	}

	# ================== HAPUS ============================

	public function hapus($box="", $id="")
	{
		switch ($box) {
			case 'inbox':
				$kotak = new kotak_masuk();
				break;

			case 'outbox':
				$kotak = new kotak_keluar();
				break;

			case 'sentbox':
				$kotak = new kotak_terkirim();
				break;

			default:
				show_404();
		}

		$kotak->delete(array('ID'=>$id));

		redirect(site_url('/sms/'.$box), 'refresh');
	}

	public function bersihkan($box=""){
		switch ($box) {
			case 'inbox':
				$kotak = new kotak_masuk();
				break;

			case 'outbox':
				$kotak = new kotak_keluar();
				break;

			case 'sentbox':
				$kotak = new kotak_terkirim();
				break;

			default:
				show_404();
		}

		$kotak::delete();

		redirect(site_url('/sms/'.$box), 'refresh');
	}

}

/* End of file utama.php */
/* Location: ./application/controllers/utama.php */